//
//  CustomScrollViews.m
//  CutomDemo
//
//  Created by Mac on 16/6/27.
//  Copyright © 2016年 关旋. All rights reserved.




//

#import "CustomScrollViews.h"

#define kScrrenW  CGRectGetWidth(self.frame)//广告的宽度
#define kScrrenH  CGRectGetHeight(self.frame)//广告的高度


@implementation CustomScrollViews
{
    UIImageView *_leftImageV;
    UIImageView *_centerImageV;
    UIImageView *_rightImageV ;
    NSArray *_image_Arr;
    NSTimer *_timer;
    NSInteger _indextNum;
    UIPageControl *_pageControl;
    
}


-(instancetype)initWithFrame:(CGRect)frame withImage:(NSArray *)imageArr withLabelText:(NSMutableArray *)textArr{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        _indextNum = 1;//默认为中心的页面;方便加一和减一
        
        _image_Arr = imageArr;

        [self createSubView];
    }
    return self;
}


-(void)createSubView{
    
    self.contentSize = CGSizeMake(kScrrenW*3, kScrrenH);
    self.showsHorizontalScrollIndicator = NO;
    self.bounces = NO;
    self.backgroundColor = [UIColor purpleColor];
    self.delegate = self;
    self.pagingEnabled = YES;
    [self setContentOffset:CGPointMake(kScrrenW, 0) animated:NO];
    //    for (int i = 0; i < 3; i ++) {
    
    _leftImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScrrenW, kScrrenH)];
    
    _leftImageV.image = _image_Arr[0];
    _centerImageV = [[UIImageView alloc]initWithFrame:CGRectMake(kScrrenW, 0, kScrrenW, kScrrenH)];
    
    _centerImageV.image = _image_Arr[1];
    
    _rightImageV = [[UIImageView alloc]initWithFrame:CGRectMake(kScrrenW*2, 0, kScrrenW, kScrrenH)];
    _rightImageV.image = _image_Arr[2];
    
    //两个图片的轮播图的时候：
    if (_image_Arr.count == 2) _rightImageV.image = _image_Arr[0];
    [self addSubview:_leftImageV];
    [self addSubview:_centerImageV];
    [self addSubview:_rightImageV];
    
    //2.
    _pageControl = [[UIPageControl alloc]init];
    _pageControl.numberOfPages = _image_Arr.count;
    _pageControl.currentPage = 0;
    _pageControl.frame = CGRectMake((kScrrenW-20*_pageControl.numberOfPages)/2, kScrrenH-20, 20*_pageControl.numberOfPages, 20);
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    _pageControl.enabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.superview addSubview:_pageControl];
    });
    
    
    //3.
    _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAct) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
    
}

-(void)timerAct{
    
    //轮播下一个页面：
    [self setContentOffset:CGPointMake(kScrrenW*2, 0) animated:YES];

    //停留下一页动画效果0.4:
    [self performSelector:@selector(scrollViewDidEndDecelerating:) withObject:self afterDelay:0.4];
    
//    NSLog(@"d定时器－－－－－－－－－－》》》》》》");
}

#pragma mark ---- scrollViewDidEndDecelerating 动画结束以后
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    if (self.contentOffset.x == 0) {
        
        //左侧：
//        _indextNum --;
//        NSLog(@"%ld",_indextNum);
        _indextNum = ((_indextNum == 0 ? 3:_indextNum)-1)%(_image_Arr.count);
//        NSLog(@"-->%ld",_indextNum);
        
    }else if (self.contentOffset.x == kScrrenW)
    {
        //中间
//        NSLog(@"%ld",_indextNum);
        return;
    }else if (self.contentOffset.x == kScrrenW*2)
    {
        //右侧：
//        _indextNum ++;
//        NSLog(@"%ld",_indextNum);
        _indextNum = (_indextNum+1)%(_image_Arr.count);
//        NSLog(@"-->%ld",_indextNum);

    }
    

    if (_indextNum == 0) {
        
        _leftImageV.image = _image_Arr[(_indextNum+2)%_image_Arr.count];
        _centerImageV.image = _image_Arr[_indextNum%_image_Arr.count];
        _rightImageV.image = _image_Arr[(_indextNum+1)%_image_Arr.count];
        
        
    }else{
        
        _leftImageV.image = _image_Arr[(_indextNum-1)%_image_Arr.count];
        _centerImageV.image = _image_Arr[_indextNum%_image_Arr.count];
        _rightImageV.image = _image_Arr[(_indextNum+1)%_image_Arr.count];
        
        
        
    }
    
//    NSLog(@"-->%ld",_indextNum);
    _pageControl.currentPage = _indextNum-1;

    if (_indextNum == 0)_pageControl.currentPage = 2;//特殊情况的时候
    
    
    [self setContentOffset:CGPointMake(kScrrenW, 0) animated:NO];

}




- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [_timer setFireDate:[NSDate distantFuture]];
    
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    

    [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:3]];
    
//    NSLog(@"666:%f",self.contentOffset.x);
    
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //顺序为1、2、0 第一个为1
//    NSLog(@"点击了图片：%ld",_indextNum);
    
}



@end
